/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jwu2.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Rolf
 */
public class Addon implements Serializable {
    private Map<String, String> addonValues;
    private List<String> addonDirectories;
    
    public Addon(String name) {
        addonValues = new HashMap<String, String>();
        addonDirectories = new ArrayList<String>();
        
        addonValues.put("name", name);
    }

    public List<String> getAddonDirectories() {
        return addonDirectories;
    }
    
    public void addAddonDirectory(String dir) {
        addonDirectories.add(dir);
    }
    
    /**
     * Checks if the addon given as parameter is newer than this one
     * @param addon
     * @return true if addon given as parameter is newer
     */
    public boolean isNewer(Addon addon) {
        long d1 = this.getDate();
        long d2 = addon.getDate();
        
        if( d2 > d1 ) {
            return true;
        }
        
        return false;
    }

    public void setDate(long date) {
        setValue("date", ""+date);
    }
    
    public long getDate() {
        return convertStringToLong(getValue("date"),0);
    }
    
    private boolean convertStringToBoolean(String s, boolean def) {
        try {
            return Boolean.parseBoolean(s);
        }
        catch(Exception e) {
            return def;
        }
    }
    
    private int convertStringToInteger(String s, int def) {
        try {
            return Integer.parseInt(s);
        }
        catch(Exception e) {
            return def;
        }
    }

    public boolean isInstalled() {
        return convertStringToBoolean(addonValues.get("installed"), false);
    }
    
    public void setValue(String key, String value) {
        addonValues.put(key, value);
    }
    
    public String getValue(String key) {
        return addonValues.get(key);
    }
    
    public String getAuthor() {
        return addonValues.get("author");
    }
    
    public String getDescription() {
        return addonValues.get("description");
    }
    
    public String getName() {
        return addonValues.get("name");
    }

    public String getDownloadUrl() {
        return addonValues.get("downloadUrl");
    }
    
    public void setDownloadUrl(String url) {
        addonValues.put("downloadUrl", url);
    }
    
    public String getId() {
        return addonValues.get("id");
    }
    
    public void setId(String id) {
        setValue("id", id);
    }

    public Set<String> getAddonValuesKeys() {
        return addonValues.keySet();
    }
    
    @Override
    public String toString() {
        return ""+getName();
    }
    
    @Override
    public boolean equals(Object o) {
        if( o instanceof Addon ) {
            Addon a = (Addon) o;
            
            if( a.getName().equals(this.getName())) {
                return true;
            }
        }
        
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + (this.getName() != null ? this.getName().hashCode() : 0);
        return hash;
    }

    private String trimForNonNumbers(String s) {
            String temp = s.replaceAll("\\.", "");
            temp = temp.replaceAll(",", "");
            temp = temp.replaceAll("-", "");
            temp = temp.replaceAll(" ", "");
            temp = temp.trim();
            
            return temp;
    }

    private long convertStringToLong(String s, long def) {
        try {
            return Long.parseLong(s);
        }
        catch(Exception e) {
            return def;
        }
    }
}
